import { verify } from '../../../utils/verify'
Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false, //控制选择报修房屋面板的显示/隐藏
    repairLayerVisible: false, // 控制选择维修项目面板的显示/隐藏
    dateLayerVisible: false,  //控制选择预约日期面板的显示/隐藏
    houseList: [], // 选择报修的房屋列表
    repairItem: [], // 选择维修的项目列表
    attachment: [], // 上传的问题附件图片
    houseInfo: '',// 选择报修的房屋信息
    repairItemName: '',// 选择要维修的项目
    houseId: '', // 已审核通过的房屋id
    repairItemId: '', // 维修项目id
    mobile: '', // 手机号
    appointment: '',// 预约日期
    description: '', //问题描述
    id:''
  },
  onLoad({ id }) {
    this.getHouseList()
    this.getRepairItem()
    this.setData({
      id: id
    })
    if (id) {
      wx.setNavigationBarTitle({
        title: '编辑报修信息',
      })
      this.getRepairDetail(id)
    }
  },
  async getRepairDetail(id) {
    if (!id) return
    const res = await wx.http.get('/repair/' + id)
    // console.log(res);
    if (res.code !== 10000) return wx.utils.toast('获取报修详情失败')
    this.setData({
      ...res.data
    }, () => {
      // 只有上门中 才做路线规划
      if (this.data.status === 2) {
        this.getPolyLine()
      }
    })
  },
  // 4. 获取报修房屋信息
  async getHouseList() {
    const res = await wx.http.get('/house')
    // console.log(res);
    if (res.code !== 10000) {
      wx.utils.toast('获取报修房屋列表失败')
    }
    this.setData({
      houseList: res.data
    })
  },
  // 5. 获取维修项目信息
  async getRepairItem() {
    const res = await wx.http.get('/repairItem')
    // console.log(res);
    this.setData({
      repairItem: res.data
    })
  },
  // 6. 文件上传完毕后会触发----上传图片
  async afterRead(e) {
    // console.log(e);
    const { file } = e.detail;
    // console.log(e.detail.file.tempFilePath);
    const res = await wx.http.upload('/upload', {
      name: 'file',
      filePath: file.tempFilePath,
      formData: {
        type: 'after-read'
      },
      header: {
        Authorization: getApp().token,
      },
    })
    // console.log(res);
    if (res.code !== 10000) {
      wx.utils.toast('附件上传失败')
    }
    // 上传完成需要更新
    const { attachment = [] } = this.data
    attachment.push({ ...file, url: res.data })
    // 渲染数据
    this.setData({ attachment })
    wx.utils.toast('附件上传成功')
  },
  // 7.删除问题附件图片
  delete(e) {
    // console.log(e.detail.index);
    this.data.attachment.splice(e.detail.index, 1)
    this.setData({
      attachment: this.data.attachment
    })
  },
  // 8. 预览图片
  previewImg(e) {
    let imgs = this.data.attachment;
    let { index } = e.currentTarget.dataset;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  // -------------打开/关闭选择报修房屋弹层---------------
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 1. 选择要保修的房屋
  selectHouse(e) {
    const { name: houseInfo, id: houseId } = e.detail
    this.setData({
      houseInfo,
      houseId
    })
  },
  // ---------------打开/关闭选择维修项目弹层-------------------
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  // 2.选择要维修的项目
  selectRepairItem(e) {
    const { name:  repairItemName, id: repairItemId } = e.detail
    this.setData({
      repairItemName,
      repairItemId
    })
  },

  // ---------------打开/关闭选择预约日期弹层------------------
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 3. 选择要预约的日期
  selectDate(e) {
    const { name: appointment } = e.detail
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatDate(e.detail)
    })
  },
  // 提交表单
  async submit() {
    // 1. 数据校验
    // 解构获取接口需要的参数
    const { houseId, repairItemId, mobile, appointment, description, attachment, id } = this.data
    // 6.1 数据校验
    if (!wx.verify.verifyHouse(houseId)) return
    if (!wx.verify.verifyRepairItem(repairItemId)) return
    if (!wx.verify.verifyMobile(mobile)) return
    if (!wx.verify.verifyDate(appointment)) return
    if (!wx.verify.verifyDesc(description)) return
    // 6.2 调用接口
    const res = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
    })
    console.log(res);
    if (res.code !== 10000) return wx.utils.toast('提交报修失败~')
    // 3 跳转到报修列表页面
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})